package ywpt.web.action.knowledge;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import ywpt.core.SystemMessage;
import ywpt.core.web.BaseAction;
import ywpt.model.KnowLib;
import ywpt.model.KnowScans;
import ywpt.service.knowledge.KnowLibService;
import ywpt.service.knowledge.KnowScansService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class KnowScansAction extends BaseAction implements
ModelDriven<KnowScans>, Preparable{

	/**
	 * 知识点附件
	 */
	private static final long serialVersionUID = -2216646597323739684L;
	private static final String URL="/knowScans/";
	private Long id;
	private Long knowLibId;
	
	private KnowScans knowScans;
	private List<KnowScans> knowScansList = new ArrayList<KnowScans>();
	private KnowScansService knowScansService;
	private KnowLib knowLib;
	private KnowLibService knowLibService;
	
	// 文件的上传
	private File[] upload;// 实际上传文件
	private String[] uploadFileName; // 上传文件名
	private String[] uploadContentType; // 文件的内容类型	

	//文件的下载
	private String fileName;
	private String fileRealName;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getKnowLibId() {
		return knowLibId;
	}

	public void setKnowLibId(Long knowLibId) {
		this.knowLibId = knowLibId;
	}

	public KnowScans getKnowScans() {
		return knowScans;
	}

	public void setKnowScans(KnowScans knowScans) {
		this.knowScans = knowScans;
	}

	public List<KnowScans> getKnowScansList() {
		return knowScansList;
	}

	public void setKnowScansList(List<KnowScans> knowScansList) {
		this.knowScansList = knowScansList;
	}

	public KnowScansService getKnowScansService() {
		return knowScansService;
	}

	public void setKnowScansService(KnowScansService knowScansService) {
		this.knowScansService = knowScansService;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}

	@Override
	public KnowScans getModel() {
		return this.knowScans;
	}

	@Override
	public void prepare() throws Exception {
		if(id != null){
			this.knowScans = knowScansService.getKnowScansById(id);
		}else{
			this.knowScans = new KnowScans();
		}
	}
	//****************************************************************
	public String delete() {
		this.knowLib = this.knowLibService.getKnowLibById(knowLibId);
 		if (!knowLib.getSubmitFlag()) {
 			this.knowScansService.removeKnowScans(id);
 		}
		return SUCCESS;
	}
		
	/**
	 *�ϴ����µĸ��� 
	 */
	public String knowScanUpload() throws Exception {
		knowLib = this.knowLibService.getKnowLibById(knowLibId);
	if(!knowLib.getCheckFlag()){
		if(!knowLib.getSubmitFlag()){
			try {
				String url = ServletActionContext.getServletContext().getRealPath(URL);
					File file_date = new File(url);
					if(!file_date.exists()){
						file_date.mkdirs();
					}	
				for (int i = 0; i < upload.length; i++) {
					if(upload[i].exists()){
						String type = uploadContentType[i]; // �ļ�����������
						String fileName = uploadFileName[i];
						String realName = UUID.randomUUID().toString() + getExt(fileName);
						File target = new File(url, realName);
						FileUtils.copyFile(upload[i], target);
						
						KnowScans ks = new  KnowScans();
						ks.setFileName(fileName);
						ks.setContentType(type);
						ks.setRealName(realName);
						this.knowScansService.saveKnowScans(ks,knowLibId);
					}
				}
				this.addActionMessage("附件已保存!");
				return SUCCESS;
			} catch (Exception e) {
				e.printStackTrace();
				this.addActionError(e.getMessage());
				return SUCCESS;
			}			
		}else{
			this.addActionMessage(SystemMessage.SUBMIT_SAVE);
			return SUCCESS;
		}
	}else{
		this.addActionMessage(SystemMessage.AUDITING_SAVE);
		return SUCCESS;
	}	
		
	}	
	
	public static String getExt(String fileName) {
		return fileName.substring(fileName.lastIndexOf("."));
	}
	/**
	 *���d֪ʶ��ĸ��� 
	 */
	public String knowScanDownload(){
		return SUCCESS;
	}
	
	public void setFileName()
	{
		// �õ��������ص��ļ���
		knowScans = this.knowScansService.getKnowScansById(id);
		this.fileName = knowScans.getRealName(); //uuid������
		this.fileRealName = knowScans.getFileName();//�ļ��ϴ���ʱ���ԭ��������
		System.out.println(fileName);
		System.out.println(fileRealName);
	}

	/*
	 * @getFileName �˷�����Ӧ����struts.xml�ļ��еģ� 
	 * <param name="contentDisposition">attachment;filename="${fileName}"</param>
	 * ����������õ������ع��������ļ�ʱ��ʾ���ļ��� Ҫ����ȷ����ʾ�����ļ���������Ҫ��fileName�ٴα���
	 * �����������ļ����������룬���޷����ص����
	 */
	public String getDownloadFileName() throws Exception
	{
		return new String(fileRealName.getBytes( ),"ISO-8859-1");
	}

	/*
	 * @getDownloadFile �˷�����Ӧ����struts.xml�ļ��еģ� <param
	 * name="inputName">downloadFile</param> ���������ļ����������Բο�struts2��Դ��
	 */
	public InputStream getDownloadFile()
	{
		this.setFileName();
		return ServletActionContext.getServletContext().getResourceAsStream(URL + fileName);
	}

	public KnowLib getKnowLib() {
		return knowLib;
	}

	public void setKnowLib(KnowLib knowLib) {
		this.knowLib = knowLib;
	}

	public KnowLibService getKnowLibService() {
		return knowLibService;
	}

	public void setKnowLibService(KnowLibService knowLibService) {
		this.knowLibService = knowLibService;
	}
	
}
